package demo_0717;

public class T7_BinarySearchTree_0410 {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        if (root == null) {
            root = new TreeNode(key);
            return true;
        }
        TreeNode prev = null;
        TreeNode cur = root;
        while (cur != null) {
            if (cur.key > key) {
                prev = cur;
                cur = cur.left;
            } else if (cur.key < key) {
                prev = cur;
                cur = cur.right;
            } else {
                return false;
            }
        }
        TreeNode node = new TreeNode(key);
        if (key > prev.key) {
            prev.right = node;
        } else {
            prev.left = node;
        }
        return true;
    }
    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (key > cur.key) {
                cur = cur.right;
            } else if (key < cur.key) {
                cur = cur.left;
            } else {
                return cur;
            }
        }
        return null;
    }
    //删除key的值
    public boolean remove(int key) {
        if (root == null) return false;
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null) {
            if (key > cur.key) {
                prev = cur;
                cur = cur.right;
            } else if (key < cur.key) {
                prev = cur;
                cur = cur.left;
            } else {
                removeNode(prev,cur);
                return true;
            }
        }
        return false;
    }

    private void removeNode(TreeNode prev, TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = root.right;
            }else if (cur == prev.left) {
                prev.left = cur.right;
            } else {
                prev.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = root.left;
            } else if (cur == prev.left) {
                prev.left = cur.left;
            } else {
                prev.right = cur.left;
            }
        } else {
            TreeNode node = cur.right;
            TreeNode nodeParent = null;
            while (node.left != null) {
                nodeParent = node;
                node = node.left;
            }
            cur.key = node.key;
            if (node == nodeParent.left) {
                nodeParent.left = node.right;
            } else {
                nodeParent.right = node.right;
            }
        }
    }
    //中序遍历即可
    public static void inorder(TreeNode root) {
        if(root == null) return;
        inorder(root.left);
        System.out.print(root.key+" ");
        inorder(root.right);
    }
    public static void main(String[] args) {
        T7_BinarySearchTree_0410 tree = new T7_BinarySearchTree_0410();
        tree.insert(5);
        tree.insert(3);
        tree.insert(4);
        tree.insert(2);
        tree.insert(7);
        tree.insert(6);
        tree.insert(8);
        tree.insert(9);
        tree.insert(10);
        tree.remove(5);
        inorder(tree.root);
    }

}
